<?php

require_once './include/common.inc.php';
require_once 'left.php';
require_once 'right.php';
include_once './include/cart.class_p.php';

$page_title = 'Acuity World WebShop';
$menu_id = 0;
$menu_str = PrintMenu();

if (!isPromoterLogin()) 
{
	header ("location:promoter_login.php");
	exit;
}

$sql = "SELECT * FROM  `promoter_info` WHERE  `id` = '" . $promoter_uid . "'";
$user_info_ary = $db->get_one($sql);

$phone = str_replace("||", " " , $user_info_ary['phone']);
$mobile = str_replace("||", " " , $user_info_ary['mobile']);
$fax = str_replace("||", " " , $user_info_ary['fax']);

$faq1_checked = $_POST['faq1'];
$faq2_checked = $_POST['faq2'];
$set_country = $_POST['set_country'];

$user_street = $_POST['user_street'];
$user_city = $_POST['user_city'];
$user_zip = $_POST['user_zip'];
$user_state = $_POST['user_state'];
$user_country = $_POST['user_country'];

// check out
if ($_POST['userdef'] == 1) {   //ûͶݵַ
	$user_street = filter($_POST['user_street']);
    $user_city = filter($_POST['user_city']);
    $user_zip = filter($_POST['user_zip']);
    $user_state = filter($_POST['user_state']);
    $user_country = filter($_POST['user_country']);
} else {
    $user_street = $user_info_ary['street2'];
    $user_city = $user_info_ary['city2'];
    $user_zip = $user_info_ary['zip2'];
    $user_state = $user_info_ary['state2'];
    $user_country = $user_info_ary['country2'];
}
$cart = new cart();
$info_ary = $cart->getData();
$total_qty = $cart->getSortCount();
$total_cost = $cart->getTotalCost();
$total_cost2 = $cart->getTotalCost2();

$cart_addvat = $cart->getAddVat();
$cart_addshippingfee = $cart->getAddShippingFee();
$cart_discount = $cart->getDisCount();
$new_total_cost=number_format($total_cost-$cart_discount,2,".","");

if($_POST["checkout_submit"]!="")
{
	if($cart->sortCount>0)
	{
	    $user_street = $_POST['user_street'];
		$user_city = $_POST['user_city'];
		$user_zip = $_POST['user_zip'];
		$user_state = $_POST['user_state'];
		$user_country = $_POST['user_country'];
		
		$cart_info_p=serialize($cart);
		$sql="INSERT INTO `promoter_order` (`promoter_id` , `country_id` , `cost` ,`cart_info`,`d_street` , `d_city` , `d_state` , `d_zip` , `d_country` ,`pay_flag`, `date` ) 
VALUES ('$promoter_uid', '$set_country', '$total_cost2', '$cart_info_p', '$user_street', '$user_city', '$user_state', '$user_zip', '$user_country', '0','$timestamp'
);";
		$orderid=$mysql->insert_id($sql);
		//send email
		$userid=$promoter_uid;
		$username = empty($user_info_ary['lastname']) ? $user_info_ary['firstname'] : $user_info_ary['firstname'] . ' ' . $user_info_ary['lastname'];
   		$email = $user_info_ary['email'];
    	$day = date("Y-m-d", $timestamp);
    	$deliveryaddress = $user_street . "<br>" . $user_city. "<br>" .$user_state. "<br>" .$user_zip. "<br>" .$COUNTRY_ARY[$user_country];
		$customaddress = $user_info_ary['street'] . "<br>" . $user_info_ary['city'] . "<br>" . $user_info_ary['state'] . "<br>" . $user_info_ary['zip'] . "<br>" . $user_info_ary['country'];
		 $cart_info_ary = $cart->getData();
		foreach ($cart_info_ary as $k=>$v) 
		{
			$price_show = $v['price'];
			$cart_list .= '<tr>
					<td class="en_black_12">'. $v['name'] .'</td>
					<td class="en_black_12">'. $price_show .'</td>
					<td class="en_black_12">'. $v['count'] .'</td>
					<td class="en_black_12">'. $v['cost'] .'</td>
				  </tr>';
		}
		$totalCost=$cart->getTotalCost();
		$discount=$cart->getDisCount();
		$new_total=number_format($totalCost-$discount,2,'.','');
		$vat_fee=$cart->getAddVat();
		$addshippingfee=$cart->getAddShippingFee();
		$totalCost2=$cart->getTotalCost2();
		
		$cart_list .= '<tr>
						<td class="en_black_12" colspan="4" align="right">
							<strong>Sub Total</strong> DKK'.$totalCost.'<br/> 
							<strong>Discount</strong> DKK'.$discount.'<br/>
							<strong>New Sub Total</strong> DKK'.$new_total.'<br/> 
							<strong>VAT</strong> DKK'.$vat_fee.'<br/> 
							<strong>Shipping Fee</strong> DKK'.$addshippingfee.'<br/> 
							<strong>Total</strong> '.$totalCost2.'</td>				
					  </tr>';
		$cart->emptyAll();
		
		/////update stock_data--------------------------------------leo
		$sql_update="";
		foreach($cart_info_ary as $key=>$val)
		{
			if($key>0)
			{
				if($val["count"]>0)
				{
					$sql_update="update pro_product set stock_data=stock_data-".$val["count"]." where id=".$key;
					$mysql->query($sql_update);
				}
			}
		}
	
		///////////////
	
		require_once './mail/order_to_promoters1.php';
    	sendhtmlmail($from_name,$from_email,$to_name,$to_email,$subject,$content);
		
		require_once './mail/order_promoter_to_webmaster1.php';
		sendhtmlmail($from_name,$from_email,$to_name,$to_email,$subject,$content);
	
		////		
		header("location:promoter_approved.php");
		exit();
	}
}


require_once PrintEot('header');
require_once PrintEot('left');
require_once PrintEot('promoter_checkout');
require_once PrintEot('right');
require_once PrintEot('footer');
?>
